package oracle.apps.financials.collection.adapter;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.cyrilmottier.android.greendroid.R;

public class ListOfValueAdapter extends BaseAdapter {
	public HashMap<Integer, Boolean> selectedMap;
	private Context context = null;
	private LayoutInflater inflater = null;
	private List<HashMap<String, Object>> list = null;
	private String keyString[] = null;
	private String itemString = null;
	private int idValue[] = null;

	public ListOfValueAdapter(Context context, List<HashMap<String, Object>> list, int resource, String[] from, int[] to) {
		this.context = context;
		this.list = list;
		keyString = new String[from.length];
		idValue = new int[to.length];

		System.arraycopy(from, 0, keyString, 0, from.length);
		System.arraycopy(to, 0, idValue, 0, to.length);

		inflater = LayoutInflater.from(this.context);

		selectedMap = new HashMap<Integer, Boolean>();
		for (int i = 0; i < list.size(); i++) {
			selectedMap.put(i, false);
		}
	}

	public int getSelectedCount() {
		int count = 0;
		for (int i = 0; i < selectedMap.size(); i++) {
			if (selectedMap.get(i)) {
				count++;
			}
		}
		return count;
	}

	
	public	boolean isSelectedAll(){		
		return getCount()==getSelectedCount();		
	}
	
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View view, ViewGroup arg2) {
		ViewHolder holder = null;
		if (holder == null) {
			holder = new ViewHolder();
			if (view == null) {
				view = inflater.inflate(R.layout.list_view_item, null);
			}
			holder.textView = (TextView) view.findViewById(R.id.item_textView);
			holder.checkbox = (CheckBox) view.findViewById(R.id.item_checkBox);
			view.setTag(holder);
		}
		HashMap<String, Object> map = list.get(position);
		if (map != null) {
			itemString = (String) map.get(keyString[0]);
			holder.textView.setText(itemString);
		}
		holder.checkbox.setChecked(selectedMap.get(position));
		return view;
	}

	public class ViewHolder {
		public TextView textView = null;
		public CheckBox checkbox = null;
	}

}
